package cn.hdmis.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import cn.hdmis.entity.Grade;
import cn.hdmis.service.GradeService;

public class GradeAction extends BaseAction{

	private static final long serialVersionUID = -2122805406585150639L;
	
	private Grade grade;
	private GradeService gradeService;
	private List<Grade> gradeList;
	private List<Map> gradeJson;
	
	public void setGradeList(List<Grade> gradeList) {
		this.gradeList = gradeList;
	}
	@JSON(name="rows")
	public List<Grade> getGradeList() {
		return gradeList;
	}
	public void setGradeJson(List<Map> tempList) {
		this.gradeJson = tempList;
	}
	public List<Map> getGradeJson() {
		return gradeJson;
	}
	@JSON(serialize=false)
	public Grade getGrade() {
		return grade;
	}
	public void setGrade(Grade grade) {
		this.grade = grade;
	}
	@JSON(serialize=false)
	public GradeService getGradeService() {
		return gradeService;
	}
	public void setGradeService(GradeService gradeService) {
		this.gradeService = gradeService;
	}
	
	public String addGrade(){
		try {
			
			gradeService.addGrade(grade);
		} catch (Exception e) {
			setMSG(MSG_FAILED);
			logger.error(e.getMessage());
		}
		return "addGrade";
	}
	
	public String loadGrade(){
		try {	
			List<Grade> gradelist = gradeService.getAllGrade();
			this.setGradeList(gradelist);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return "gradeList";
	}
	
    public String loadGradeJson(){
    	try {	
    		List<Grade> gradelist = gradeService.getAllGrade();
    		List<Map> tempList = new ArrayList<Map>();
    		for (Grade grade : gradelist) {
    			Map<String,String>  valueMap = new HashMap<String, String>();
    			valueMap.put("id",grade.getId().toString());
    			valueMap.put("text",grade.getName());
    			tempList.add(valueMap);
			}
    		this.setGradeJson(tempList);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
    	return "json";
    }
    
	public void delGrade(){
		
		String grade_id = this.getRequest().getParameter("grade_id");
		try {	
			gradeService.delGrade(Integer.parseInt(grade_id));
			
		} catch (Exception e) {
			setMSG(MSG_FAILED);
			logger.error(e.getMessage());
		}
		responseJson(getMSG());
	}
}
